package code

import (
	"fmt"
	"gf-start-kit/library/jtime"
	"github.com/gogf/gf/frame/g"
	"strconv"
	"strings"
	"time"
)

var (
	CGJH = "CGJH"
	PFFH = "PFFH"
)
var Prefix = g.MapStrAny{
	CGJH: "bill",
}

//获取一个新code
func NewCode(key string) string {
	r, _ := g.DB().Table(Prefix[key].(string)).Order("code desc,createtime desc").FindOne("createtime between ? and ?", jtime.TodayStart(), jtime.TodayEnd())
	code := ""
	num := 0
	if r["code"].String() != "" {
		s := strings.Split(r["code"].String(), "-")
		num, _ = strconv.Atoi(s[3])
	}
	code = fmt.Sprintf("%s-%s-%s-%04d", key, time.Now().Format("20060102"), "001", num+1)
	return code
}

//传入一个code获取下一个code值
func NextCode(code string) string {
	num := 0
	if code != "" {
		s := strings.Split(code, "-")
		num, _ = strconv.Atoi(s[3])
		code = fmt.Sprintf("%s-%s-%s-%04d", s[0], time.Now().Format("20060102"), "001", num+1)
		fmt.Println(code)

		return code
	}
	return ""
}
